<?php
/**
 * @file userdynamics.php
 * @author LJ (liangjian@haibao.com)
 * @date 2016年4月27日 上午11:12:43
 * @brief 
 */
namespace haibao\user\data;

use haibao\frame\data\query\Condition;

class UserDynamics extends \haibao\user\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\user\model\data\UserDynamics');
        $this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
    }
    
    public function getUserDynamics($userId, $page = 1, $limit = 15){
        $cache = new \haibao\user\cache\UserDynamics();
        $retArr = (array)$cache->getUserDynamics($userId);
        if(!$retArr){
            $dynamicsFilter = new \haibao\user\model\filter\UserDynamics();
            $dynamicsFilter->select(array(
                $dynamicsFilter::CONDITION_FIELD_NAME_MSGCONTENT,
                $dynamicsFilter::CONDITION_FIELD_NAME_CREATETIME
            ))
            ->where($dynamicsFilter::CONDITION_FIELD_NAME_USERID, Condition::CONDITION_EQUAL, $userId)
            ->order($dynamicsFilter::CONDITION_FIELD_NAME_CREATETIME, false)
            ->setPager(new \haibao\frame\data\query\Pager($limit, $page));
            
            $dynamicsList = $this->getDynamics($dynamicsFilter);
            if($dynamicsList){
                foreach($dynamicsList as $key=>$item){
                    array_push($retArr, array(
                        'MsgContent' => json_decode($item->MsgContent, true),
                        'CreateTime' => $item->CreateTime->format('Y-m-d H:i:s')
                    ));
                }
            }
            $cache->setUserDynamics($userId, $retArr);
        }
        return $retArr;
    }
    
    public function getDynamics($query){
        return $this->getAll($query);
    }
    
}